package com.dubito.dose.di

import android.app.Application
import androidx.room.Room
import com.dubito.dose.data.MedicationDatabase
import com.dubito.dose.data.repository.MedicationRepository
import com.dubito.dose.data.repository.MedicationRepositoryImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor

/**
 * author: LiLiang
 * createTime: 2022/9/5-13:48
 * remark: 让Hilt管理依赖关系
 **/

@Module
@InstallIn(SingletonComponent::class)
object MedicationDataModule {

    /**
     * 创建OkHttp
     */
    @Provides
    @Singleton
    fun provideOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder().addInterceptor(
            HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            }
        ).build()
    }

    /**
     * 创建room数据库
     */
    @Provides
    @Singleton
    fun provideMedicationDatabase(app: Application): MedicationDatabase {
        return Room.databaseBuilder(app, MedicationDatabase::class.java, "medication_db").build()
    }

    @Provides
    @Singleton
    fun provideMedicationRepository(
        db: MedicationDatabase
    ): MedicationRepository {
        return MedicationRepositoryImpl(dao = db.dao)
    }

}